﻿using CPPEI.Coolzon.UI.WinForm;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.IO.Pipes;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace CPPEI.Coolzon.UI.ActiveXPartLib
{
    /// <summary>
    /// Interaction logic for INSVideoPart.xaml
    /// 调用./tools/CPPEI.Coolzon.Tools.INSVideoTool.exe “url” "uid" "pwd" "camera"
    /// 来显示INSVideo
    /// </summary>
    [System.ComponentModel.DisplayName("INSVideo组件")]
    [XPartIcon("pack://application:,,,/CPPEI.Coolzon.UI.ActiveXPartLib;component/Images/PartIcons/INS监视系统.png")]
    [XPart("INSVideoPart", "INSVideo组件")]
    public partial class INSVideoPart : ActiveXPart
    {
        private INSVideoProgramWindowHost _ctlWFHost;
        private SingletonINSVideoPro _pro;

        private ILoadINSStrategy _loadStrategy;

        #region xproperties

        [Category("Other")]
        [DisplayName("地址")]
        [Description("Url")]
        [Editor(typeof(XProperty.Editors.XStringEditor), typeof(XProperty.XPropertyValueEditor))]
        [XProperty(XPropertyCategory.Other, "Url", "地址")]
        public string XUrl
        {
            get;
            set;
        }
        [Category("Other")]
        [DisplayName("用户名")]
        [Description("UserName")]
        [Editor(typeof(XProperty.Editors.XStringEditor), typeof(XProperty.XPropertyValueEditor))]
        [XProperty(XPropertyCategory.Other, "UserName", "用户名")]
        public string XUserName
        {
            get;
            set;
        }
        [Category("Other")]
        [DisplayName("密码")]
        [Description("Password")]
        [Editor(typeof(XProperty.Editors.XStringEditor), typeof(XProperty.XPropertyValueEditor))]
        [XProperty(XPropertyCategory.Other, "Password", "密码")]
        public string XPassword
        {
            get;
            set;
        }

        [Category("Other")]
        [DisplayName("摄像头Id")]
        [Description("CameraID")]
        [Editor(typeof(XProperty.Editors.XStringEditor), typeof(XProperty.XPropertyValueEditor))]
        [XProperty(XPropertyCategory.Other, "CameraID", "摄像头Id")]
        public int XCameraID
        {
            get
            {
                return (int)GetValue(CameraIdProperty);
            }
            set
            {
                SetValue(CameraIdProperty, value);
            }
        }
        [Category("Other")]
        [DisplayName("视频显示模式")]
        [Description("Mode")]
        [Editor(typeof(XProperty.Editors.XStringEditor), typeof(XProperty.XPropertyValueEditor))]
        [XProperty(XPropertyCategory.Other, "Mode", "视频显示模式")]
        public int? XMode
        {
            get;
            set;
        }
        [Category("Other")]
        [DisplayName("登录视频服务器策略")]
        [Description("LoginClientStrategy")]
        [Editor(typeof(XProperty.Editors.XStringEditor), typeof(XProperty.XPropertyValueEditor))]
        [XProperty(XPropertyCategory.Other, "LoginClientStrategy", "登录视频服务器策略")]
        public string XLoginClientStrategy
        {
            get;
            set;
        }
        #endregion

        #region DependencyPropery
        private static DependencyProperty CameraIdProperty = DependencyProperty.Register("XCameraID", typeof(int), typeof(INSVideoPart), new PropertyMetadata(OnCameraIdPropertyChangedCallback));

        private static void OnCameraIdPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            INSVideoPart insVidelPart = d as INSVideoPart;
            insVidelPart.ChangeCameraID((int)e.NewValue);
            insVidelPart.Refresh();
        }
        [Category("Other")]
        [DisplayName("本地视频文件")]
        [Description("LocalVideoFileName")]
        [Editor(typeof(XProperty.Editors.XStringEditor), typeof(XProperty.XPropertyValueEditor))]
        [XProperty(XPropertyCategory.Other, "LocalVideoFileName", "本地视频文件")]
        public ResourceFile XLocalVideoFileName
        {
            get { return (ResourceFile)GetValue(LocalVideoFileNameProperty); }
            set { SetValue(LocalVideoFileNameProperty, value); }
        }
        private static DependencyProperty LocalVideoFileNameProperty = DependencyProperty.Register("LocalVideoFileName", typeof(ResourceFile), typeof(INSVideoPart), new PropertyMetadata(OnLocalVideoFileNamePropertyChangedCallback));

        private static void OnLocalVideoFileNamePropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            INSVideoPart insVidelPart = d as INSVideoPart;
            insVidelPart.Refresh();
        }
        #endregion

        public INSVideoPart()
        {
            InitializeComponent();
        }

        protected override void OnActiveXInit(PartContext context)
        {
            if(this.PartMetadata.IsPreload)
            {
                _loadStrategy = new PreloadINSStrategy();
        }
            else
            {
                _loadStrategy = new NormalloadINSStrategy();
            }
        }

        protected override void OnActiveXActive(PartContext context)
        {
            if (this.IsVisibleAndParentVisible())
            {
                try
                {
                    if (_ctlWFHost == null)
                    {
                        _ctlWFHost = new INSVideoProgramWindowHost();
                        _ctlLayout.Children.Add(_ctlWFHost);

                        _pro = _loadStrategy.Start();

                        if (this._pro != null)
                        {
                            this._pro.Opened += _pro_Opened;
                            this._pro.OpenCamera(XUrl, XUserName, XPassword, XCameraID, XLoginClientStrategy, XLocalVideoFileName.Path,
                                this.Name,
                                this.PartContext.SceneMetadata.Name);
                        }
                    }
                }
                catch (Exception ex)
                {
                    Common.Logger.ErrorToTag("UI", "INSVideo part:{0} OnActive Error:{1}", this.Name, ex);
                }
            }
        }

        void _pro_Opened(string scene, string part, IntPtr hwnd)
        {
            this.Dispatcher.Invoke(() =>
            {
                if (this.PartContext != null && this.PartContext.SceneMetadata.Name == scene && this.Name == part)
                {
                    Common.Logger.DebugToTag("UI", "INSVideo part:{0} _pro_Opened hwnd:{1}", this.Name, hwnd);
                    _ctlWFHost.Start(_pro, hwnd);
                    this._pro.Opened -= _pro_Opened;
                }
            });
        }

        protected override void OnActiveXDeactive(PartContext context)
        {
            if (_ctlWFHost != null && _pro != null)
            {
                _loadStrategy.CloseCamera(this._ctlWFHost.appWin);
                _loadStrategy.Stop();
                _ctlWFHost.Stop();
                _ctlWFHost = null;
                _ctlLayout.Children.Clear();
                _pro = null;
            }
        }

        protected override void OnDestory()
        {
            if (_ctlWFHost != null && _pro != null)
            {
                _loadStrategy.CloseCamera(this._ctlWFHost.appWin);
                _loadStrategy.Stop();
                _ctlWFHost.Stop();
                _ctlWFHost = null;
                _pro = null;

                _ctlLayout.Children.Clear();
            }
            base.OnDestory();
        }

        private void ChangeCameraID(int p)
        {
            if (IsActived && _ctlWFHost != null)
            {
                _pro.SetCamera(_ctlWFHost.appWin, XCameraID);
            }
        }
    }
}
